package com.tianjishuju.cimiss.station.station.api;


import com.tianjishuju.cimiss.station.station.manager.RealTimeService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/getdata")
public class DataFromDataBas {
    private static Logger logger = Logger.getLogger(DataFromDataBas.class);

    @Autowired
    private RealTimeService realTimeService;


    /**
     * 实况数据-当期气温排序
     * @return
     */
    @RequestMapping("/getRealTempSort")
    public List<Map<String,Object>> getTempSort(@RequestParam("time") String time){
        List<Map<String,Object>> realTempSort = realTimeService.getRealTempSort(time);
        return realTempSort;
    }

    /**
     * 实况数据-降水实况
     * @return
     */
    @RequestMapping("/getRealRinAuto")
    public Object getRealRinAuto(@RequestParam("time") String time){
        return realTimeService.getRealRinAuto(time);
    }
    /**
     * 实况数据-气温实况
     * @return
     */
    @RequestMapping("/getRealTemp")
    public Object getRealTemp(@RequestParam("time") String time){
        return realTimeService.getRealTemp(time);
    }
    /**
     * 实况数据-小时降水
     * @return
     */
    @RequestMapping("/getRealRain")
    public Object getRealRain(@RequestParam("time") String time){
            return realTimeService.getRealRain(time);
    }
    /**
     * 实况数据-气,湿,温
     * @return
     */
    @RequestMapping("/getRealRirHumTemp")
    public Object getRealRirHumTemp(@RequestParam("time") String time,@RequestParam("staId") String staId){
        return realTimeService.getRealRirHumTemp(time,staId);
    }
    /**
     * 实况数据-风速,风向
     * @return
     */
    @RequestMapping("/getRealWindAndDir")
    public Object getRealWindAndDir(@RequestParam("time") String time,@RequestParam("staId") String staId){
        return realTimeService.getRealWindAndDir(time,staId);
    }
    /**
     * 实况数据-能见度（长江航道 读取excel）
     * @return
     */
    @RequestMapping("/getRealVisibility")
    public Object getRealVisibility(@RequestParam("time") String time){
        return realTimeService.getRealVisibility(time);
    }
    /**
     * 实况数据-L雷达
     * @return
     */
    @RequestMapping("/getRealRadar")
    public Object getRealRadar(@RequestParam("time") String time){
        return realTimeService.getRealRadar(time);
    }

    /**
     * 雷达流传输
     * @param datacode
     * @return
     */
    @RequestMapping("/getRealRadarFlow")
    public void getRealRadarFlow(@RequestParam("datacode") String datacode, HttpServletResponse response){
        URL resource = this.getClass().getResource("/");
        File imageFile = new File(resource.getPath() + "images/"+datacode+".gif");// 创建文件对象
        if(imageFile.isFile()) {
            try (InputStream in=new FileInputStream(imageFile);
                 OutputStream out = response.getOutputStream();) {
                response.setContentType("image/gif");

                byte[] temp = new byte[1024];
                int size = in.read(temp);
                while (size != -1) {
                    out.write(temp);
                    size = in.read(temp);
                }

            } catch (IOException e) {
                logger.error("传输数据失败", e);
            }
        };
    }

    /**
     * 实况数据-风速,风向,气,湿,温 站点信息
     * @return
     */
    @RequestMapping("/getExelSite")
    public Object getExelSite(){
        return realTimeService.getExelSite();
    }

    /**
     * 实况数据——全部数据
     * @return
     */
    @RequestMapping("/getRealTimeSer")
    public Map<String,Object> getRealTimeSer(@RequestParam("time") String time){
        Map<String,Object> rtn = new HashMap<>();
        Map<String,Object> all = realTimeService.findAllDisplayMainSer("",time);
        rtn.put("list",all);
        return rtn;
    }


}
